package com.sky.controller.admin;

import com.sky.dto.DishAddDTO;
import com.sky.dto.DishDTO;
import com.sky.dto.DishPageQueryDTO;
import com.sky.entity.Dish;
import com.sky.result.PageResult;
import com.sky.result.Result;
import com.sky.service.DishService;
import com.sky.vo.DishPageQueryItemVO;
import com.sky.vo.DishVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.constraints.NotNull;
import javax.validation.constraints.Size;
import java.util.List;

/**
 * 菜品管理
 * */
@Validated
@Api(tags = "菜品管理")
@RestController
@RequestMapping("/admin/dish")
public class DishController {


    @Autowired
    private DishService dishService;

    /**
     * 新增菜品
     * */
    @ApiOperation("新增菜品")
    @PostMapping
    public Result add(@Validated @RequestBody DishAddDTO dto){
        dishService.add(dto);
        return Result.success();
    }

    /**
     * 菜品分页查询
     * */
    @ApiOperation("菜品分页查询")
    @GetMapping("/page")
    public Result<PageResult<DishPageQueryItemVO>> page(DishPageQueryDTO dto){
        PageResult<DishPageQueryItemVO> page = dishService.page(dto);
        return Result.success(page);
    }

    /**
     * 根据id列表批量删除菜品
     * */
    @ApiOperation("根据id列表批量删除菜品")
    @DeleteMapping
    public Result<Void> delete(@RequestParam("ids")
                                   @NotNull(message = "ids不能为空")
                                   @Size(min =1,message = "至少上传1个id")
                                   List<Long> ids){
        dishService.delete(ids);
        return Result.success();
    }

    /**
     * 根据id查询菜品
     * */
    @ApiOperation("根据id查询菜品")
    @GetMapping("{id}")
    public Result<DishVO> getById(@PathVariable("id")Long id){
        DishVO vo = dishService.getById(id);
        return Result.success(vo);
    }

    /**
     * 根据id修改菜品
     * */
    @ApiOperation("根据id修改菜品")
    @PutMapping
    public Result<Void> update(@Validated @RequestBody DishDTO dto){
        dishService.update(dto);
        return Result.success();
    }



    /**
     * 根据分类id查询菜品
     * @param categoryId
     * @return
     */
    @GetMapping("/list")
    @ApiOperation("根据分类id查询菜品")
    public Result<List<Dish>> list(Long categoryId){
        List<Dish> list = dishService.list(categoryId);
        return Result.success(list);
    }

    /**
     * 起售停售
     * */
    @PostMapping("/status/{status}")
    public Result enableOrDisable(@PathVariable("status")Integer status, @RequestParam("id")Long id){
        dishService.enableOrDisable(id, status);
        return Result.success();
    }


}
